//
//  LiveingViewController.m
//  SinaWebo
//
//  Created by 王方伟 on 16/7/21.
//  Copyright © 2016年 亦心. All rights reserved.
//

#import "LiveingViewController.h"
#import "Mblog.h"
#import "PageInfo.h"
#import "MediaInfo.h"
#import "User.h"
#import "UIImageView+Extension.h"
#import <IJKMediaFramework/IJKMediaFramework.h>

@interface LiveingViewController ()
@property (strong, nonatomic) IJKFFMoviePlayerController *moviePlayer;
@property (weak, nonatomic) IBOutlet UIImageView *headImg;
@property (weak, nonatomic) IBOutlet UILabel *userNameL;
//粒子动画
@property (weak, nonatomic) CAEmitterLayer *emitterLayer;
@end

@implementation LiveingViewController

- (CAEmitterLayer *)emitterLayer{
    if (_emitterLayer == nil) {
        CAEmitterLayer *emitterLayer = [CAEmitterLayer layer];
        // 发射器在xy平面的中心位置
        emitterLayer.emitterPosition = CGPointMake(self.moviePlayer.view.frame.size.width-50,self.moviePlayer.view.frame.size.height-50);
        // 发射器的尺寸大小
        emitterLayer.emitterSize = CGSizeMake(20, 20);
        // 渲染模式
        emitterLayer.renderMode = kCAEmitterLayerUnordered;
        // 开启三维效果
        //    _emitterLayer.preservesDepth = YES;
        NSMutableArray *array = [NSMutableArray array];
        // 创建粒子
        for (int i = 0; i<10; i++) {
            // 发射单元
            CAEmitterCell *stepCell = [CAEmitterCell emitterCell];
            // 粒子的创建速率，默认为1/s
            stepCell.birthRate = 1;
            // 粒子存活时间
            stepCell.lifetime = arc4random_uniform(4) + 1;
            // 粒子的生存时间容差
            stepCell.lifetimeRange = 1.5;
            // 颜色
            // fire.color=[[UIColor colorWithRed:0.8 green:0.4 blue:0.2 alpha:0.1]CGColor];
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"good%d_30x30", i]];
            // 粒子显示的内容
            stepCell.contents = (id)[image CGImage];
            // 粒子的名字
            //            [fire setName:@"step%d", i];
            // 粒子的运动速度
            stepCell.velocity = arc4random_uniform(100) + 100;
            // 粒子速度的容差
            stepCell.velocityRange = 80;
            // 粒子在xy平面的发射角度
            stepCell.emissionLongitude = M_PI+M_PI_2;;
            // 粒子发射角度的容差
            stepCell.emissionRange = M_PI_2/6;
            // 缩放比例
            stepCell.scale = 0.3;
            [array addObject:stepCell];
        }
        
        emitterLayer.emitterCells = array;
        [self.moviePlayer.view.layer addSublayer: emitterLayer];
        _emitterLayer = emitterLayer;
    }
    return _emitterLayer;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)setBlog:(Mblog *)blog{
    _blog = blog;
    
    [self setVideoPlay];
    
    [self setTopView];
    
    self.emitterLayer.hidden = NO;
    
}

- (void)setVideoPlay{
    IJKFFOptions *options = [IJKFFOptions optionsByDefault];
    [options setPlayerOptionIntValue:1  forKey:@"videotoolbox"];
    
    // 帧速率(fps) （可以改，确认非标准桢率会导致音画不同步，所以只能设定为15或者29.97）
    [options setPlayerOptionIntValue:29.97 forKey:@"r"];
    // -vol——设置音量大小，256为标准音量。（要设置成两倍音量时则输入512，依此类推
    [options setPlayerOptionIntValue:512 forKey:@"vol"];
    _moviePlayer = [[IJKFFMoviePlayerController alloc] initWithContentURLString:_blog.page_info.media_info.stream_url withOptions:options];
    _moviePlayer.view.frame = self.view.bounds;
    [self.view insertSubview:_moviePlayer.view atIndex:0];
    // 填充fill
    _moviePlayer.scalingMode = IJKMPMovieScalingModeAspectFill;
    // 设置自动播放(必须设置为NO, 防止自动播放, 才能更好的控制直播的状态)
    _moviePlayer.shouldAutoplay = YES;
    // 默认不显示
    _moviePlayer.shouldShowHudView = YES;
    
    [_moviePlayer prepareToPlay];
    [_moviePlayer play];
}

- (void)setTopView{
    User *user = _blog.user;
    _userNameL.text = user.screen_name;
    [_headImg fw_setImage:[NSURL URLWithString:user.profile_image_url]];
}

- (IBAction)onClick{
    [_moviePlayer stop];
    [_moviePlayer shutdown];
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
